package vn.buffalocoders.rikos.ui.home;



import vn.buffalocoders.rikos.R;
import vn.buffalocoders.rikos.ui.category.CategoryActivity;
import vn.buffalocoders.rikos.ui.home.indicator.CirclePageIndicator;
import vn.buffalocoders.rikos.ui.home.indicator.PageIndicator;
import vn.buffalocoders.rikos.ui.login.LogInActivity;
import vn.buffalocoders.rikos.ui.signup.SignUpActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;


public class HomeActivity extends FragmentActivity implements OnClickListener { // @travistrle@gmail.com

	ViewPager pager;
	FragmentStatePagerAdapter adapter; 
	LinearLayout mTitleGroup;
	PageIndicator mIndicator;
	/* Just some random URLs
	 * 
	 * Each page of our pager will display one URL from this array
	 * Swiping, to the right will take you to the next page having
	 * the next URL.
	 */
	String[] toVisit={
			"http://m.vnexpress.net",
			"http://www.google.com",
			"http://www.adsangtao.com",
			"http://www.genk.vn?mobile=true",
			"http://www.haivl.com",
	};

	Fragment[] mSlidingPages = new Fragment[toVisit.length];
	
	public void hideTitleGroup () {
		mTitleGroup.setVisibility(View.GONE);
	}
	
	public int getLength() {
		return toVisit.length;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		pager=(ViewPager)findViewById(R.id.home);
		mTitleGroup = (LinearLayout)findViewById(R.id.title_group);

		adapter=new FragmentStatePagerAdapter(
				getSupportFragmentManager()
				){

			@Override
			public int getCount() {
				// This makes sure getItem doesn't use a position
				// that is out of bounds of our array of URLs
				return toVisit.length;
			}

			@Override
			public Fragment getItem(int position) {
				// Here is where all the magic of the adapter happens
				// As you can see, this is really simple.
				return mSlidingPages[position];
			}
		};

		// letruong@netvisiontel.co.kr - start
        logoutReceiver = new LogoutReceiver();
        
        // Register the logout receiver
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.package.ACTION_LOGOUT");
        registerReceiver(logoutReceiver, intentFilter);
		// letruong@netvisiontel.co.kr - end
		//Let the pager know which adapter it is supposed to use
		pager.setOffscreenPageLimit(getLength());
		pager.setAdapter(adapter);
		mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
		mIndicator.setViewPager(pager);

		for(int i = 0; i < getLength(); i++) {
			if(mSlidingPages[i] == null) {
				mSlidingPages[i] = BrowserFragment.newInstance(HomeActivity.this, toVisit[i]);
			}
		}
		
		// travistrle@gmail.com
		findViewById(R.id.registerBtn).setOnClickListener(this);
		findViewById(R.id.loginHomeBtn).setOnClickListener(this);
		findViewById(R.id.skipBtn).setOnClickListener(this);
	}

	// travistrle@gmail.com
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.registerBtn) {
			Intent intent = new Intent(HomeActivity.this, SignUpActivity.class);
			startActivity(intent);			
		}
		else if (v.getId() == R.id.loginHomeBtn) {
			Intent intent = new Intent(HomeActivity.this, LogInActivity.class);
			startActivity(intent);			
		}
		else if (v.getId() == R.id.skipBtn) {
			Intent intent = new Intent(HomeActivity.this, CategoryActivity.class);
			startActivity(intent);
		}
	}
	
	// test
	public class LogoutReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.package.ACTION_LOGOUT")) {
                finish();
            }
        }
	}
	 
    private LogoutReceiver logoutReceiver;
 
    @Override
    protected void onDestroy() {
        // Unregister the logout receiver
        unregisterReceiver(logoutReceiver);
        super.onDestroy();
    }
}